package com.sfzd5.exam.helplibrary.taskstatisitcs;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PieChartHelper {
    JFreeChart mPieChart;
    DefaultPieDataset dataSet;
    ChartPanel jp;

    public ChartPanel geChartPanel() {
        return jp;
    }

    public PieChartHelper(TotalInfo totalInfo){
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN", false);
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋体",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋体",Font.PLAIN,15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

        //ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",true));
        mPieChart = ChartFactory.createPieChart("成绩统计", getData(totalInfo), true, true, false);

        PiePlot pieplot = (PiePlot)mPieChart.getPlot();
        pieplot.setLabelFont(new Font("宋体", 0, 12));
        pieplot.setNoDataMessage("无数据");
        pieplot.setCircular(true);
        pieplot.setLabelGap(0.02D);
        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} {2}",
                NumberFormat.getNumberInstance(),
                new DecimalFormat("0.00%")));

        jp = new ChartPanel(mPieChart);
        jp.setSize(300, 300);
    }

    public void resetPieChart(TotalInfo totalInfo){
        //模拟数据
        PiePlot plot = (PiePlot) mPieChart.getPlot();
        plot.setDataset(getData(totalInfo));
    }

    private DefaultPieDataset getData(TotalInfo totalInfo){
        if(dataSet==null)
            dataSet = new DefaultPieDataset();//创建数据集
        dataSet.setValue("优秀",totalInfo.total优秀);//设置数据
        dataSet.setValue("满分",totalInfo.total满分);
        dataSet.setValue("及格",totalInfo.total及格);
        dataSet.setValue("不及格",totalInfo.total不及格);
        return dataSet;
    }
}
